package com.niit.session19;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;

public class StudentDao {
	
	public static void main(String[] args) {
		StudentDao dao = new StudentDao();
		try {
			boolean addStudent = dao.addStudent(new Student("2","Trout","男",2,1));
			if(addStudent) {
				System.out.println("添加成功");
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 添加学生
	 * @param student
	 * @return
	 * @throws SQLException
	 */
	public boolean addStudent(Student student) throws SQLException {
//		打开数据库链接
		Connection conn = JDBCUtils.getConn();
		Statement statement = conn.createStatement();
		String sql = "insert into student value ('" + student.getSid() + "','" + student.getName() + "','" + student.getGender()
				+ "',"+student.getBatch()+","+student.getGroupLeader()+")";
		System.out.println(sql);
//		executeUpdate会返回一个int类型的变量，这个变量指的是执行SQL语句之后会影响的行数
		int update = statement.executeUpdate(sql);
		JDBCUtils.close(conn, statement, null);
		return update > 0 ? true : false;
	}

}
